package com.czhi.lian;

import com.lmax.disruptor.BlockingWaitStrategy;
import com.lmax.disruptor.EventFactory;
import com.lmax.disruptor.RingBuffer;
import com.lmax.disruptor.Sequence;
import com.lmax.disruptor.dsl.Disruptor;
import com.lmax.disruptor.dsl.ProducerType;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Main {
    public static void main(String[] args) throws InterruptedException {
        final ExecutorService es = Executors.newFixedThreadPool(5);
        final ExecutorService executor = Executors.newFixedThreadPool(5);
        Disruptor<Trade> disruptor = new Disruptor<Trade>(() -> new Trade(),
                1024 * 1024,
                executor,
                ProducerType.SINGLE,
                new BlockingWaitStrategy());
        // 2.把消费者设置到Disruptor中handleEventsWith
//        disruptor.handleEventsWith(new TradeHandle1(), new TradeHandle2());     //并行执行
//        disruptor.handleEventsWith(new TradeHandle1()).handleEventsWith(new TradeHandle2());        //串行执行
        /**
         * 菱形操作(一)
         */
//        disruptor.handleEventsWith(handler1,handler2).handleEventsWith(handler3);
        /**
         * 菱形操作(二)
         */
//        disruptor.handleEventsWith(handler1, handler2).then(handler3);

        // 3.启动disruptor
        final RingBuffer<Trade> ringBuffer = disruptor.start();
        long begin = System.currentTimeMillis();
        CountDownLatch latch = new CountDownLatch(1);
        es.submit(new TradePushlisher(disruptor,latch));
        latch.await();

        disruptor.shutdown();
        executor.shutdown();
        es.shutdown();
        System.out.println("总耗时：" + (System.currentTimeMillis() - begin));
    }
}
